package com.siha_back.controller;


import com.siha_back.client.DeepSeekClient;
import com.siha_back.common.FileTextExtractor;
import com.siha_back.common.Result;
import com.siha_back.entity.dto.PageBean;
import com.siha_back.entity.dto.Resume;
import com.siha_back.service.ResumeService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import java.util.List;
import java.util.logging.Logger;


@RestController
@RequestMapping("/resume")
public class ResumeController {

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @Autowired
    private ResumeService resumeService;

    @Autowired
    private DeepSeekClient deepSeekClient;
    private static final Logger log = Logger.getLogger(ResumeController.class.getName());
    /*// 单文件上传,简历修改
    /**
     * ai简历回复
     *
     * @return 回答是否成功
     */
    @PostMapping("/upload")
    public Result handleSingleFileUpload(
            @RequestParam("file") MultipartFile file
           ) throws Exception {

        try {
            byte[] bytes = file.getBytes();
            System.out.println("文件内容（前100字节）: " + new String(bytes, 0, Math.min(100, bytes.length)));
            FileTextExtractor extractor = new FileTextExtractor();
            String text=extractor.extractText(file.getBytes(), file.getOriginalFilename());
            return Result.success(200,"success",deepSeekClient.singleTurnAsk("请帮我修改简历"+text));
        } catch (Exception e) {
            return Result.fail("fail",e.getMessage());
        }
    }
    /**
     * 查询单简历详情
     *
     * @return 查询是否成功
     */
    @GetMapping("/detail")
    public Result<Resume> queryById(@RequestParam("id") Integer id) {
        String result = resumeService.queryById(id);

        return Result.success(200,result,null);
    }

    /**
     * 删除简历
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public Result deleteById(@RequestParam("id")Integer id) {
        boolean b = resumeService.deleteById(id);
        if(!b){
            return Result.error("删除失败");
        }
        return Result.success(200,"删除成功",null);
    }

    /**
     * 查询所有简历
     *
     * @return 查询是否成功
     */
    @GetMapping
    public Result<List<Resume>> list(){
        List<Resume> cs = resumeService.list();
        if(cs == null){
            return Result.error("查询失败");
        }
        return Result.success(200,"成功",cs);
    }

    /**
     * 查询所有简历并且分页
     *
     * @return 查询是否成功
     */
    @GetMapping("/list1")
    public Result<PageBean<Resume>> list1(
            @RequestParam(value = "pageNum")Integer pageNum,
            @RequestParam(value = "pageSize")Integer pageSize,
            @RequestParam(value = "resumeName",required = false)String resumeName,
            @RequestParam(value = "resumeType",required = false)String resumeType){
        System.out.println(resumeName);
        System.out.println(pageNum);
        System.out.println(pageSize);
        PageBean<Resume> pb = resumeService.list1(pageNum,pageSize,resumeName,resumeType);
        if (pb==null){
            return Result.error("查询失败");
        }
        System.out.println(pb);
        return Result.success(200,"成功",pb);
    }

}
